# -*- coding:utf-8 -*-

from libs.handler import BaseHandler
from libs.sae_fts import SaeFts
from tornado.escape import json_encode

class Search(BaseHandler):
	def get(self):
		query = self.get_argument("query","").encode("utf8")
		
		page = self.get_argument("start_page",1)
		data = self._getData(query, page)
		
		items = []
		if data["count"]>0:
			items = self.db.query("SELECT id,title,price,pic_height,\
                num_iid,like_count,reply_count from items WHERE id IN %s", data["data"])
		
		if page>1:
			ret = {}
			ret["page"] = page
			ret['stat'] = 'ok'
			ret['pages'] = 22
			ret["data"] = []
			for val in items:
				val['price'] = str(val['price'])
				html = self.render_string("module/item.html", item = val)
				ret["data"].append(html)
				  
			self.write(json_encode(ret))
			return True
				
		self.render("search.html", query = query, count = data["count"], items = items )

	def _getData(self, query, page):
		search_data= SaeFts().search(query, "file_long1", "asced", 12, page)
		return search_data

handlers = []
handlers = [(r"/search",Search)]
